﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using UFC.Common;

namespace TransferMesReportForm
{
    partial class TransferMesReportForm : ServiceBase
    {
        //定时器,不同业务报表不同的定时器，同一个业务的不同报表可共用一个定时器
        private System.Timers.Timer m_tExample;//例子

        private System.Timers.Timer m_TransferShipBoard;//转工排产看板
        private System.Timers.Timer m_TransferCheckCyclebBoard;//检验周期看板
        private System.Timers.Timer m_TransferMfgorderTouLiaoBoard;//订单投料看板
        private System.Timers.Timer m_TransferEmployeeWorkTimeBoard;//同步工时数据
        private System.Timers.Timer m_TransferContainerStepPlanBoard;//同步分厂工序排产完成率看板

        //业务类
        TransferShipBoard tsb = new TransferShipBoard();//转工排产看板
        TransferCheckCyclebBoard tcb = new TransferCheckCyclebBoard();//检验周期看板
        TransferMfgorderTouLiaoBoard tmb = new TransferMfgorderTouLiaoBoard();//订单投料看板
        TransferEmployeeWorkTimeBoard twb = new TransferEmployeeWorkTimeBoard();//同步工时数据
        TransferContainerStepPlanBoard tcsp = new TransferContainerStepPlanBoard();//分厂工序排产完成率看板

        public TransferMesReportForm()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            LogHelp.SetConfig();
            LogHelp.WriteLog("报表服务启动");
            int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());

            //转工排产看板
            SyncTimerStatr(ref m_TransferShipBoard, m_TransferShipBoard_Elapsed, intSync * 5 * 60);//服务器30分钟，本地5分钟

            //检验周期看板
            SyncTimerStatr(ref m_TransferCheckCyclebBoard, m_TransferCheckCyclebBoard_Elapsed, intSync * 5 * 60);//服务器30分钟，本地5分钟

            //订单投料看板
            SyncTimerStatr(ref m_TransferMfgorderTouLiaoBoard, m_TransferMfgorderTouLiaoBoard_Elapsed, intSync * 30 * 60);//服务器30分钟，本地5分钟

            //同步工时
            SyncTimerStatr(ref m_TransferEmployeeWorkTimeBoard, m_TransferEmployeeWorkTimeBoard_Elapsed, intSync * 1 * 45 * 60);

            //分厂工序排产完成率看板
            SyncTimerStatr(ref m_TransferContainerStepPlanBoard, m_TransferContainerStepPlanBoard_Elapsed, intSync * 60 * 5);//服务器30分钟，本地5分钟
        }
        
        //传参启动新线程并运行方法
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync * 1000;
            timer.Enabled = true;
            timer.Start();
        }
        
        //例子线程
        private void m_tExample_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_tExample.Enabled = false;
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("例子线程", myError);
            }
            finally
            {
                m_tExample.Enabled = true;
            }
        }

        //转工排产看板
        private void m_TransferShipBoard_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_TransferShipBoard.Enabled = false;
                tsb.RunMain();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("TransferShipBoard服务-转工排产看板", myError);
            }
            finally
            {
                m_TransferShipBoard.Enabled = true;
            }
        }

        //检验周期看板
        private void m_TransferCheckCyclebBoard_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_TransferCheckCyclebBoard.Enabled = false;
                tcb.RunMain();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("TransferCheckCyclebBoard服务-检验周期看板", myError);
            }
            finally
            {
                m_TransferCheckCyclebBoard.Enabled = true;
            }
        }

        //订单投料统计看板
        private void m_TransferMfgorderTouLiaoBoard_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_TransferMfgorderTouLiaoBoard.Enabled = false;
                tmb.RunMain();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("TransferShipBoard服务-订单投料统计看板", myError);
            }
            finally
            {
                m_TransferMfgorderTouLiaoBoard.Enabled = true;
            }
        }

        //同步工时统计
        private void m_TransferEmployeeWorkTimeBoard_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                //凌晨3点执行 其他时间不执行
                if (DateTime.Now.Hour != 3)
                    return;
                m_TransferEmployeeWorkTimeBoard.Enabled = false;
                twb.RunMain();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("TransferEmployeeWorkTimeBoard服务-工时统计", myError);
            }
            finally
            {
                m_TransferEmployeeWorkTimeBoard.Enabled = true;
            }
        }

        //同步分厂工序排产完成率看板(生产作业会)
        private void m_TransferContainerStepPlanBoard_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_TransferContainerStepPlanBoard.Enabled = false;
                tcsp.RunMain();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("TransferContainerStepPlanBoard服务-分厂工序排产完成率看板(生产作业会)", myError);
            }
            finally
            {
                m_TransferContainerStepPlanBoard.Enabled = true;
            }
        }

        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }
    }
}